package com.zc.ui;

import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;

public class GameJFrame extends JFrame implements KeyListener, ActionListener {
    JMenu qiehuan = new JMenu("更换图片");
    JMenuItem replayItem = new JMenuItem("重新开始");
    JMenuItem reloginItem = new JMenuItem("重新登录");
    JMenuItem closeItem = new JMenuItem("退出游戏");

    JMenuItem animal= new JMenuItem("动物");
    JMenuItem girl= new JMenuItem("美女");
    JMenuItem sport= new JMenuItem("运动");

    JMenuItem aboutItem = new JMenuItem("关于我");
    int[][] data = new int[4][4];//存放数据
    int x = 0;//空白位置
    int y = 0;
    String[] strings = {"animal", "girl", "sport"};
    Random r = new Random();
    String s=strings[r.nextInt(3)];
    String path = "image\\"+s+"\\"+s+(r.nextInt(8)+1)+"\\";

    int[][] win={
        {1,2,3,4},
        {5,6,7,8},
        {9,10,11,12},
        {13,14,15,0}
    };
    int count=0;
    //主界面
    public GameJFrame() {
        initJFrame();  //初始化界面
        iniJMenuBar();//初始化菜单
        initData();//初始化数据
        initImage();//初始化图片

        this.setVisible(true);  //显示
    }

    private void initData() {
        int[] temp = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
        Random random = new Random();
        for (int i = 0; i < temp.length; i++) {
            int index = random.nextInt(temp.length);
            int temp1 = temp[i];
            temp[i] = temp[index];
            temp[index] = temp1;
        }
        for (int i = 0; i < temp.length; i++) {
            if (temp[i] == 0) {
                x = i / 4;
                y = i % 4;
            }
            data[i / 4][i % 4] = temp[i];
        }
    }//加载数据

    private void initImage() {

        this.getContentPane().removeAll();
        if(Win()){
            JLabel win = new JLabel(new ImageIcon("image\\win.png"));
            win.setBounds(203, 283, 197, 73);
            this.getContentPane().add(win);
        }
        JLabel label = new JLabel("步数："+count);
        label.setBounds(50, 30, 100, 20);
        this.getContentPane().add(label);
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                int number = data[i][j];
                JLabel jLabel = new JLabel(new ImageIcon(path + number + ".jpg"));
                jLabel.setBounds(105 * j + 83, 105 * i + 134, 105, 105);
                jLabel.setBorder(new BevelBorder(BevelBorder.LOWERED));
                this.getContentPane().add(jLabel);
            }
        }
        JLabel baackground = new JLabel(new ImageIcon("image\\background.png"));
        baackground.setBounds(40, 40, 508, 560);
        this.getContentPane().add(baackground);
        this.getContentPane().repaint();
    }//加载图片


    private void iniJMenuBar() {
        JMenuBar jMenuBar = new JMenuBar();
        JMenu functionjMenu = new JMenu("功能");
        JMenu aboutjMenu = new JMenu("关于我");
        functionjMenu.add(qiehuan);
        functionjMenu.add(replayItem);
//        functionjMenu.add(reloginItem);
        functionjMenu.add(closeItem);

        aboutjMenu.add(aboutItem);

        qiehuan.add(animal);
        qiehuan.add(girl);
        qiehuan.add(sport);
        animal.addActionListener(this);
        girl.addActionListener(this);
        sport.addActionListener(this);

        replayItem.addActionListener(this);
//        reloginItem.addActionListener(this);
        closeItem.addActionListener(this);
        aboutItem.addActionListener(this);
        jMenuBar.add(functionjMenu);
        jMenuBar.add(aboutjMenu);
        this.setJMenuBar(jMenuBar);
    }//加载工具栏


    private void initJFrame() {
        this.setSize(603, 680);  //界面宽高
        this.setTitle("拼图 v1.0");  //标题
        this.setAlwaysOnTop(true);  //置顶
        this.setLocationRelativeTo(null);//显示在屏幕中央
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//关闭窗口
        this.setLayout(null);
        this.addKeyListener(this);//键盘全局监听器
    }//加载主页

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        int code = e.getKeyCode();
        if(code==65){
            this.getContentPane().removeAll();
            JLabel  all= new JLabel(new ImageIcon(path+"all.jpg"));
            all.setBounds(83,134,420,420);
            this.getContentPane().add(all);
            JLabel baackground = new JLabel(new ImageIcon("image\\background.png"));
            baackground.setBounds(40, 40, 508, 560);
            this.getContentPane().add(baackground);
            this.getContentPane().repaint();
        }
    }//键盘监听，按下不松开

    @Override
    public void keyReleased(KeyEvent e) {
        if(Win()){
            return;
        }
        int code = e.getKeyCode();
        if(code==37){
            System.out.println("向左移动");
            if(y==3){
                return;
            }
            data[x][y]=data[x][y+1];
            data[x][y+1]=0;
            y++;
            count++;
            initImage();
        }else if(code==38){
            System.out.println("向上移动");
            if(x==3){
                return;
            }
            data[x][y]=data[x+1][y];
            data[x+1][y]=0;
            x++;
            count++;
            initImage();
        }else if(code==39){
            System.out.println("向右移动");
            if(y==0){
                return;
            }
            data[x][y]=data[x][y-1];
            data[x][y-1]=0;
            y--;
            count++;
            initImage();
        }else if(code==40){
            System.out.println("向下移动");
            if(x==0){
                return;
            }
            data[x][y]=data[x-1][y];
            data[x-1][y]=0;
            x--;
            count++;
            initImage();
        } else if (code==65) {
            initImage();
        }else if(code==87){
            data = new int[][]{
                    {1,2,3,4},
                    {5,6,7,8},
                    {9,10,11,12},
                    {13,14,15,0}
            };
            initImage();
        }
    }//键盘监听。按下松开

    public boolean Win(){
        for (int i = 0; i < data.length; i++) {
            for (int j = 0; j < data[i].length; j++) {
                if(data[i][j]!=win[i][j]){
                    return false;
                }
            }
        }
        return true;
    }//判断是否通关
    @Override
    public void actionPerformed(ActionEvent e) {
      if(e.getSource()==replayItem){
          System.out.println("重新游戏");
          count=0;
          initData();
          initImage();
      } else if (e.getSource()==reloginItem) {
          System.out.println("重新登录");
          this.setVisible(false);
          new LoginJFrame();
      }else if(e.getSource()==closeItem){
          System.out.println("退出游戏");
          System.exit(0);
      }else if(e.getSource()==aboutItem){
          System.out.println("关于作者");
          JDialog dialog = new JDialog();
          JLabel jLabel= new JLabel(new ImageIcon("image\\about.png"));
          jLabel.setBounds(0,0,258,258);
          dialog.getContentPane().add(jLabel);
          dialog.setSize(344,344);
          dialog.setAlwaysOnTop(true);
          dialog.setLocationRelativeTo(null);
          dialog.setModal(true);
          dialog.setVisible(true);
      } else if (e.getSource()==girl) {
          path="image\\girl\\girl"+(r.nextInt(8)+1)+"\\";
          count=0;
          initData();
          initImage();
      }else if(e.getSource()==animal){
          path="image\\animal\\animal"+(r.nextInt(8)+1)+"\\";
          count=0;
          initData();
          initImage();
      }else if(e.getSource()==sport){
          path="image\\sport\\sport"+(r.nextInt(8)+1)+"\\";
          count=0;
          initData();
          initImage();
      }
    }//鼠标监听
}
